package com.yuqing.sentimentapp.workspace.epidemic.action;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.androidkun.xtablayout.XTabLayout;
import com.linkface.baselibrary.http.BaseBean;
import com.yuqing.sentimentapp.R;
import com.yuqing.sentimentapp.base.BaseActivity;
import com.yuqing.sentimentapp.base.BaseObserver;
import com.yuqing.sentimentapp.module.ModuleManager;
import com.yuqing.sentimentapp.workspace.epidemic.action.entity.EpidemicActionInfoResult;
import com.yuqing.sentimentapp.workspace.epidemic.action.entity.EpidemicActionReportResult;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 疫情外参
 * Created by shenhe on 2020/4/3.
 */
public class EpidemicActionFillInActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.tv_save)
    TextView mTvSave;
    @BindView(R.id.et_fire_action_count)
    EditText mEtFireActionCount;
    @BindView(R.id.et_fire_action_person_count)
    EditText mEtFireActionPersonCount;
    @BindView(R.id.et_fire_action_car_count)
    EditText mEtFireActionCarCount;
    @BindView(R.id.ll_fire)
    LinearLayout mLlFire;
    @BindView(R.id.et_ctl_action_count)
    EditText mEtCtlActionCount;
    @BindView(R.id.et_ctl_action_person_count)
    EditText mEtCtlActionPersonCount;
    @BindView(R.id.et_ctl_action_car_count)
    EditText mEtCtlActionCarCount;
    @BindView(R.id.ll_ctl)
    LinearLayout mLlCtl;
    @BindView(R.id.et_assist_action_count)
    EditText mEtAssistActionCount;
    @BindView(R.id.et_assist_action_person_count)
    EditText mEtAssistActionPersonCount;
    @BindView(R.id.et_assist_action_car_count)
    EditText mEtAssistActionCarCount;
    @BindView(R.id.ll_assist)
    LinearLayout mLlAssist;
    @BindView(R.id.et_other_action_count)
    EditText mEtOtherActionCount;
    @BindView(R.id.et_other_action_person_count)
    EditText mEtOtherActionPersonCount;
    @BindView(R.id.et_other_action_car_count)
    EditText mEtOtherActionCarCount;
    @BindView(R.id.ll_other)
    LinearLayout mLlOther;
    @BindView(R.id.et_total_action_person_count)
    EditText mEtTotalActionPersonCount;
    @BindView(R.id.et_total_action_car_count)
    EditText mEtTotalActionCarCount;
    @BindView(R.id.ll_total)
    LinearLayout mLlTotal;
    @BindView(R.id.et_reportor)
    EditText mEtReportor;
    @BindView(R.id.et_auditor)
    EditText mEtAuditor;
    @BindView(R.id.ll_place)
    LinearLayout mLlPlace;


    private boolean editEnable = true;
    private EpidemicActionInfoResult.DataBean actionInfo;
    private EpidemicActionReportResult.DataBean.SubSumBean subSum;
    /**
     * 从已上报列表跳转过来的
     */
    private EpidemicActionReportResult.DataBean.ReportsBean reportsInfo;

    public static void goActivity(Activity activity, EpidemicActionInfoResult.DataBean actionInfo, EpidemicActionReportResult.DataBean.SubSumBean subSum) {
        Intent intent = new Intent(activity, EpidemicActionFillInActivity.class);
        intent.putExtra("actionInfo", actionInfo);
        intent.putExtra("subSum", subSum);
        activity.startActivity(intent);
    }

    public static void goActivity(Activity activity, EpidemicActionReportResult.DataBean.ReportsBean reportsInfo) {
        Intent intent = new Intent(activity, EpidemicActionFillInActivity.class);
        intent.putExtra("reportsInfo", reportsInfo);
        activity.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_epidemic_action_fill_in;
    }

    @Override
    public void initPresenter() {
        Intent intent = getIntent();
        if (intent == null) return;
        actionInfo = ((EpidemicActionInfoResult.DataBean) intent.getSerializableExtra("actionInfo"));
        subSum = ((EpidemicActionReportResult.DataBean.SubSumBean) intent.getSerializableExtra("subSum"));

        reportsInfo = ((EpidemicActionReportResult.DataBean.ReportsBean) intent.getSerializableExtra("reportsInfo"));
    }

    @Override
    public void initView() {
        if (actionInfo != null) {
            mEtFireActionCount.setText(actionInfo.getFireActionCount());
            mEtFireActionCarCount.setText(actionInfo.getFireActionCarCount());
            mEtFireActionPersonCount.setText(actionInfo.getFireActionPersonCount());

            mEtCtlActionCount.setText(actionInfo.getCtlActionCount());
            mEtCtlActionCarCount.setText(actionInfo.getCtlActionCarCount());
            mEtCtlActionPersonCount.setText(actionInfo.getCtlActionPersonCount());

            mEtAssistActionCount.setText(actionInfo.getAssistActionCount());
            mEtAssistActionCarCount.setText(actionInfo.getAssistActionCarCount());
            mEtAssistActionPersonCount.setText(actionInfo.getAssistActionPersonCount());

            mEtOtherActionCount.setText(actionInfo.getOtherActionCount());
            mEtOtherActionCarCount.setText(actionInfo.getOtherActionCarCount());
            mEtOtherActionPersonCount.setText(actionInfo.getOtherActionPersonCount());

            mEtTotalActionCarCount.setText(actionInfo.getTotalCarCount());
            mEtTotalActionPersonCount.setText(actionInfo.getTotalPersonCount());

            mEtAuditor.setText(actionInfo.getAuditor());
            mEtReportor.setText(actionInfo.getReportor());
        } else {
            if (subSum != null) {
                mEtFireActionCount.setText(subSum.getFireActionCount());
                mEtFireActionCarCount.setText(subSum.getFireActionCarCount());
                mEtFireActionPersonCount.setText(subSum.getFireActionPersonCount());

                mEtCtlActionCount.setText(subSum.getCtlActionCount());
                mEtCtlActionCarCount.setText(subSum.getCtlActionCarCount());
                mEtCtlActionPersonCount.setText(subSum.getCtlActionPersonCount());

                mEtAssistActionCount.setText(subSum.getAssistActionCount());
                mEtAssistActionCarCount.setText(subSum.getAssistActionCarCount());
                mEtAssistActionPersonCount.setText(subSum.getAssistActionPersonCount());

                mEtOtherActionCount.setText(subSum.getOtherActionCount());
                mEtOtherActionCarCount.setText(subSum.getOtherActionCarCount());
                mEtOtherActionPersonCount.setText(subSum.getOtherActionPersonCount());

                mEtTotalActionCarCount.setText(subSum.getTotalCarCount());
                mEtTotalActionPersonCount.setText(subSum.getTotalPersonCount());
            }
        }

        /**
         * 从已上报列表跳转过来的，不可编辑，隐藏保存按钮
         */
        if (reportsInfo != null) {
            mEtFireActionCount.setText(reportsInfo.getFireActionCount());
            mEtFireActionCarCount.setText(reportsInfo.getFireActionCarCount());
            mEtFireActionPersonCount.setText(reportsInfo.getFireActionPersonCount());

            mEtCtlActionCount.setText(reportsInfo.getCtlActionCount());
            mEtCtlActionCarCount.setText(reportsInfo.getCtlActionCarCount());
            mEtCtlActionPersonCount.setText(reportsInfo.getCtlActionPersonCount());

            mEtAssistActionCount.setText(reportsInfo.getAssistActionCount());
            mEtAssistActionCarCount.setText(reportsInfo.getAssistActionCarCount());
            mEtAssistActionPersonCount.setText(reportsInfo.getAssistActionPersonCount());

            mEtOtherActionCount.setText(reportsInfo.getOtherActionCount());
            mEtOtherActionCarCount.setText(reportsInfo.getOtherActionCarCount());
            mEtOtherActionPersonCount.setText(reportsInfo.getOtherActionPersonCount());

            mEtTotalActionCarCount.setText(reportsInfo.getTotalCarCount());
            mEtTotalActionPersonCount.setText(reportsInfo.getTotalPersonCount());

            mEtAuditor.setText(reportsInfo.getAuditor());
            mEtReportor.setText(reportsInfo.getReportor());

            mEtFireActionCount.setEnabled(false);
            mEtFireActionCarCount.setEnabled(false);
            mEtFireActionPersonCount.setEnabled(false);

            mEtCtlActionCount.setEnabled(false);
            mEtCtlActionCarCount.setEnabled(false);
            mEtCtlActionPersonCount.setEnabled(false);

            mEtAssistActionCount.setEnabled(false);
            mEtAssistActionCarCount.setEnabled(false);
            mEtAssistActionPersonCount.setEnabled(false);

            mEtOtherActionCount.setEnabled(false);
            mEtOtherActionCarCount.setEnabled(false);
            mEtOtherActionPersonCount.setEnabled(false);

            mEtTotalActionCarCount.setEnabled(false);
            mEtTotalActionPersonCount.setEnabled(false);

            mEtAuditor.setEnabled(false);
            mEtReportor.setEnabled(false);

            mTvSave.setVisibility(View.GONE);
        }

        TextWatcher carCountWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    int fireActionCarCount = Integer.parseInt(mEtFireActionCarCount.getText().toString());
                    int ctlActionCarCount = Integer.parseInt(mEtCtlActionCarCount.getText().toString());
                    int assistActionCarCount = Integer.parseInt(mEtAssistActionCarCount.getText().toString());
                    int otherActionCarCount = Integer.parseInt(mEtOtherActionCarCount.getText().toString());
                    mEtTotalActionCarCount.setText(fireActionCarCount + ctlActionCarCount + assistActionCarCount + otherActionCarCount + "");
                } catch (Exception e) {
                    mEtTotalActionCarCount.setText("NaN");
                }
            }
        };
        mEtFireActionCarCount.addTextChangedListener(carCountWatcher);
        mEtCtlActionCarCount.addTextChangedListener(carCountWatcher);
        mEtAssistActionCarCount.addTextChangedListener(carCountWatcher);
        mEtOtherActionCarCount.addTextChangedListener(carCountWatcher);

        TextWatcher personCountWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    int fireActionPersonCount = Integer.parseInt(mEtFireActionPersonCount.getText().toString());
                    int ctlActionPersonCount = Integer.parseInt(mEtCtlActionPersonCount.getText().toString());
                    int assistActionPersonCount = Integer.parseInt(mEtAssistActionPersonCount.getText().toString());
                    int otherActionPersonCount = Integer.parseInt(mEtOtherActionPersonCount.getText().toString());
                    mEtTotalActionPersonCount.setText(fireActionPersonCount + ctlActionPersonCount + assistActionPersonCount + otherActionPersonCount + "");
                } catch (Exception e) {
                    mEtTotalActionPersonCount.setText("NaN");
                }
            }
        };
        mEtFireActionPersonCount.addTextChangedListener(personCountWatcher);
        mEtCtlActionPersonCount.addTextChangedListener(personCountWatcher);
        mEtAssistActionPersonCount.addTextChangedListener(personCountWatcher);
        mEtOtherActionPersonCount.addTextChangedListener(personCountWatcher);

    }

    @OnClick({R.id.iv_back, R.id.tv_save})
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_save:
                if (TextUtils.isEmpty(mEtReportor.getText().toString())) {
                    showShortToast("填报人不可为空");
                    return;
                }
                if (TextUtils.isEmpty(mEtAuditor.getText().toString())) {
                    showShortToast("审核人不可为空");
                    return;
                }
                if (actionInfo == null) {
                    actionInfo = new EpidemicActionInfoResult.DataBean();
                    setData();
                    saveReportInfo(actionInfo);
                } else {
                    setData();
                    updateReportInfo(actionInfo);
                }
                break;
        }
    }

    private void setData() {
        actionInfo.setFireActionCount(TextUtils.isEmpty(mEtFireActionCount.getText().toString()) ? "0" : mEtFireActionCount.getText().toString());
        actionInfo.setFireActionCarCount(TextUtils.isEmpty(mEtFireActionCarCount.getText().toString()) ? "0" : mEtFireActionCarCount.getText().toString());
        actionInfo.setFireActionPersonCount(TextUtils.isEmpty(mEtFireActionPersonCount.getText().toString()) ? "0" : mEtFireActionPersonCount.getText().toString());

        actionInfo.setCtlActionCount(TextUtils.isEmpty(mEtCtlActionCount.getText().toString()) ? "0" : mEtCtlActionCount.getText().toString());
        actionInfo.setCtlActionCarCount(TextUtils.isEmpty(mEtCtlActionCarCount.getText().toString()) ? "0" : mEtCtlActionCarCount.getText().toString());
        actionInfo.setCtlActionPersonCount(TextUtils.isEmpty(mEtCtlActionPersonCount.getText().toString()) ? "0" : mEtCtlActionPersonCount.getText().toString());

        actionInfo.setAssistActionCount(TextUtils.isEmpty(mEtAssistActionCount.getText().toString()) ? "0" : mEtAssistActionCount.getText().toString());
        actionInfo.setAssistActionCarCount(TextUtils.isEmpty(mEtAssistActionCarCount.getText().toString()) ? "0" : mEtAssistActionCarCount.getText().toString());
        actionInfo.setAssistActionPersonCount(TextUtils.isEmpty(mEtAssistActionPersonCount.getText().toString()) ? "0" : mEtAssistActionPersonCount.getText().toString());

        actionInfo.setOtherActionCount(TextUtils.isEmpty(mEtOtherActionCount.getText().toString()) ? "0" : mEtOtherActionCount.getText().toString());
        actionInfo.setOtherActionCarCount(TextUtils.isEmpty(mEtOtherActionCarCount.getText().toString()) ? "0" : mEtOtherActionCarCount.getText().toString());
        actionInfo.setOtherActionPersonCount(TextUtils.isEmpty(mEtOtherActionPersonCount.getText().toString()) ? "0" : mEtOtherActionPersonCount.getText().toString());

        actionInfo.setTotalCarCount(mEtTotalActionCarCount.getText().toString());
        actionInfo.setTotalPersonCount(mEtTotalActionPersonCount.getText().toString());
        actionInfo.setAuditor(mEtAuditor.getText().toString());
        actionInfo.setReportor(mEtReportor.getText().toString());
    }

    private void updateReportInfo(EpidemicActionInfoResult.DataBean actionInfo) {
        ModuleManager.getInstance().updateEpidemicActionReportInfoResult(actionInfo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    protected void hideDialog() {
                        stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(BaseBean baseBean) {
                        showShortToast("保存成功");
                        finish();
                    }

                    @Override
                    protected void onFailure(String message) {
                        showShortToast(message);
                    }
                });
    }

    private void saveReportInfo(EpidemicActionInfoResult.DataBean actionInfo) {
        ModuleManager.getInstance().saveEpidemicActionReportInfoResult(actionInfo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    protected void hideDialog() {
                        stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(BaseBean baseBean) {
                        showShortToast("保存成功");
                        finish();
                    }

                    @Override
                    protected void onFailure(String message) {
                        showShortToast(message);
                    }
                });
    }
}
